﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using SyndicateBusiness.Classes;
using SyndicateDAO.DatabaseObject;

namespace BigLotterySyndicate.Secured.Draws.Controls
{
    public partial class TabLinesHealth : Base
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Visible)
                return;


            this.linkAddLine.NavigateUrl = String.Format("/Secured/Draws/?dId={0}&Tab=3", this.CurrentDraw.id);

            this.linkAddOldLines.NavigateUrl = String.Format("/Secured/Draws/?dId={0}&Tab=6", this.CurrentDraw.id);

            this.linkAddLine.Visible = this.DrawOpen && RemainingFunds > 0;
            linkAddOldLines.Visible = this.DrawOpen && RemainingFunds > 0;

            this.ButtonRemove.Visible = !this.DrawLocked;
            this.PlaceHolderGlobalCheckBox.Visible = this.ButtonRemove.Visible;

            SetList();
        }



        private void SetList()
        {
            if (Page.IsPostBack)
                return;

            BindList();
        }

        private void BindList()
        {
            var query = this.Lines;

            if (query.Count() > 0)
            {
                this.RepeaterList.DataSource = query;
                this.RepeaterList.ItemDataBound += new RepeaterItemEventHandler(RepeaterList_ItemDataBound);
                this.RepeaterList.DataBind();

                this.MultiViewList.SetActiveView(this.ViewList);
            }
            else
            {
                this.MultiViewList.SetActiveView(this.ViewEmpty);
            }
        }

        void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            line l = e.Item.DataItem as line;

            ((System.Web.UI.HtmlControls.HtmlInputCheckBox)e.Item.FindControl("chkAction")).Value = String.Format("lineId_{0}", l.id);

            ((PlaceHolder)e.Item.FindControl("PlaceHolderAction")).Visible = this.DrawOpen;

            // if (this.Lines.Count() > e.Item.ItemIndex + 1)
            //    ((PlaceHolder)e.Item.FindControl("PlaceHolderAction")).Visible = false;



        }

        #region print

        public string PrintResults()
        {
            if (this.DrawFinished)
            {
                List<string> numbers = this.BasePage.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id).Split(',').ToList();

                List<int> a = new List<int>();

                int counter = 1;
                foreach (string item in numbers.Where(x=> x != String.Empty))
                {
                    a.Add(Int32.Parse(item));
                    
                    counter++;
                }

                a.Sort();


                StringBuilder builder = new StringBuilder();

                foreach (int n in a)
                {
                    if (n < 100)
                        builder.AppendFormat("<div class=\"no-match\">{0}</div>", n);
                    else
                        builder.AppendFormat("<div class=\"bonus-match\">{0}</div>", n - 100);
                }
                builder.Append("</div>");

                return builder.ToString();

            }
            else
            {
                return "No results yet, please be patient.";
            }



        }

        public string PrintNumbers(Object data)
        {
            line l = data as line;

            List<string> numbers = l.numbers.Split(',').ToList();

            List<int> a = new List<int>();

            foreach (string item in numbers)
            {
                a.Add(Int32.Parse(item));
            }


            a.Sort();

            if (CurrentDraw.status == draw.drawstatus.FINISHED)
            {
                int totalMatch = 0;
                bool bonusMatch = false;

                StringBuilder builder = new StringBuilder();

                builder.Append("<div class=\"numbers\">");
                foreach (int n in a)
                {
                    if (this.Result.Contains(n))
                    {
                        builder.AppendFormat("<div class=\"match\">{0}</div>", n);
                        totalMatch++;
                    }
                    else if (n == this.ResultBonusBall)
                    {
                        builder.AppendFormat("<div class=\"bonus-match\">{0}</div>", n);
                        bonusMatch = true;
                    }
                    else
                    {
                        builder.AppendFormat("<div class=\"no-match\">{0}</div>", n);
                    }



                }

                if (totalMatch >= 5 && bonusMatch)
                    totalMatch++;

                builder.Append("</div>");
                builder.AppendFormat("<div class=\"result\">{0}</div>", totalMatch);

                return builder.ToString();

            }
            else
            {

                StringBuilder builder = new StringBuilder();

                builder.Append("<div class=\"numbers\">");
                foreach (int n in a)
                {
                    builder.AppendFormat("<div class=\"no-match\">{0}</div>", n);
                }
                builder.Append("</div>");
                builder.AppendFormat("<div class=\"result\">&nbsp;</div>");

                return builder.ToString();
            }

        }

        #endregion

        protected void ButtonRemove_Click(object sender, EventArgs e)
        {
            bool actionPeformed = false;



            ControlCollection repeaterRows = this.FindControl("RepeaterList").Controls;
            HtmlInputCheckBox cb;

            foreach (Control row in repeaterRows)
            {
                cb = (System.Web.UI.HtmlControls.HtmlInputCheckBox)row.FindControl("chkAction");
                if (cb.Checked)
                {
                    int? ObjectId = Utilities.StringToInt(cb.Value.Replace("lineId_", ""));

                    if (ObjectId.HasValue)
                    {
                        line l = this.BasePage.BusinessFacade.LineBL.GetSingle(ObjectId.Value);

                        if (l != null)
                        {

                            int lid = l.id;

                            if (this.BasePage.BusinessFacade.LineBL.DeleteLine(l))
                            {
                                InsertEventLogEntry("Line [" + lid.ToString() + "] removed.");
                                if (!actionPeformed)
                                    actionPeformed = true;
                            }
                        }
                    }
                }

            }

            if (actionPeformed)
            {

                this.BindList();


                this.linkAddLine.Visible = this.DrawOpen && RemainingFunds > 0;
                linkAddOldLines.Visible = this.DrawOpen && RemainingFunds > 0;
            }
        }
    }
}